﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using Test_LYK.DataAccess;
using Test_LYK.Models;

namespace Test_LYK.Repository
{
    public class EatLogReposiroty
    {
        public bool AddEadLog(EatLogModel eat)
        {
            var db = new EatEntities();
            var date = DateTime.Now;
            db.EatLog.Add(new EatLog() {persionId = eat.PersionId, foodId = eat.FoodId, CreateOn = date,Hide = false});

            return db.SaveChanges() > 0;
        }

        public List<EatLogModel> GetEatLog()
        {
            var db = new EatEntities();

            var eatLog = (from log in db.EatLog
                join person in db.Person on log.persionId equals person.Id
                join food in db.Food on log.foodId equals food.Id
                select new EatLogModel
                {
                    Id = log.Id,
                    CreateOn = log.CreateOn,
                    FoodId = food.Id,
                    FoodName = food.Food1,
                    PersionId = person.Id,
                    PersonName = person.Name,
                    Hide = log.Hide
                }).Where(s=>s.Hide == false).ToList();
            //var logList = db.EatLog.Where(s=>s.Hide).Select(s=>new EatLogModel(){PersionId = })

            return eatLog;
        }

        public bool HideLog(int logId)
        {
            var db = new EatEntities();
            var log = db.EatLog.FirstOrDefault(s => s.Id == logId);
            log.Hide = !log.Hide;

            return db.SaveChanges() > 0;
        }
    }
}